<template>
    <div class="search_page">
        <!-- 用户输入搜索框 -->
        <div class="userSearch">
            <div class="userInpDiv">
                <i class="iconfont icon-sousuo sousuo"></i>
                <input type="text" id="userInp" name="search" placeholder="搜索歌曲、歌手、专辑" autocomplete="off" v-model="keyword" @input="searchKw()" @focus="focus()">
                <i class="iconfont icon-guanbi guanbi" v-show="show" @click="cancle()"></i>
            </div>
        </div>
        <!-- 搜索结果 -->
        <div class="searchC">
            <!-- 热门搜索 -->
            <div class="hotSearch" v-if="keyword == ''">
                <h3 class="hot_title">热门搜索</h3>
                <ul class="hot_list clearfix">
                    <li class="hot_item" v-for="( item,i) in hotRank" :key="i" @click="hotInps(item.first)"><a class="hot_link">{{item.first}}</a></li>
                </ul>
            </div>
            <!-- 搜索内容 -->
            <div class="searchContent" v-if="keyword != '' && scShow">
                <h3 class="searchTitle" @click="searchInp()">搜索“{{keyword}}”</h3>
                <ul class="searchMatch">
                    <li class="SMatch_li" v-for="(s,i) in suggest" :key="i" @click="Inps(s)">
                        <i class="iconfont icon-sousuo"></i>
                        <span class="songs_name">{{s.name}} - <span v-for="(singer,index) in s.artists" class="s_singer">{{singer.name}}</span></span>
                    </li>
                </ul>
            </div>
            <!-- 搜索结果 -->
            <div class="searchAnswer" v-if="!scShow">
                <!-- 最佳搜索 -->
                <!--<div class="bestAnswer">-->
                    <!--<ul>-->
                        <!--<li></li>-->
                    <!--</ul>-->
                <!--</div>-->
                <!-- 搜索结果 -->
                <div class="searchSongs">
                    <a v-for="(songs,index) in searchSongs" :key="index" class="songs_li" @click="toPlayer(songs.id,searchSongs,index)">
                       <div>
                           <p class="songN">{{songs.name}}</p>
                           <p class="songDec"><span v-for="(names,i) in songs.artists" class="s_singer">{{names.name}}</span> - {{songs.album.name}}</p>
                           <i class="iconfont icon-bofang"></i>
                       </div>
                    </a>
                </div>
            </div>
        </div>

    </div>

</template>

<script>
    /* eslint-disable */
    export default {
        name: "search",
        data(){
            return{
                hotRank:[],
                keyword:"",
                suggest:[],
                show:false,
                searchSongs:[],
                scShow:true,
            };
        },
        mounted(){
            let self = this;
            this.axios.get("search/hot?timestamp=222").then(
                function (res) {
                    console.log(res.data.result.hots);
                    self.hotRank = res.data.result.hots;
                });
        },
        methods:{
            searchKw(){
                this.showCancle();
                this.axios.get("search/suggest?keywords="+this.keyword+"&timestamp=222").then(
                    (res)=>{
                        if(res.data.result.songs !== undefined){
                            this.suggest = res.data.result.songs;
                            console.log("suggest",this.suggest);
                        }
                    }
                )
            },
            showCancle(){
                if(this.keyword.length !== 0){
                    return this.show = true;
                }else{
                    return this.show = false;

                }
            },
            cancle(){
                this.scShow = true;
                return this.keyword = "";
            },
            searchInp(){
                this.scShow = false;
                this.axios.get("search?keywords="+ this.keyword +"&limit=20"+"&offset=0").then(
                    (res)=>{
                        console.log("搜索结果",res.data.result.songs);
                        this.searchSongs = res.data.result.songs;
                    }
                );
            },
            focus(){
                return this.scShow = true;
            },
            Inps(want){
                this.keyword = want.id;
                this.searchInp();
                this.keyword = want.name;
            },
            hotInps(want){
                this.keyword = want;
                this.searchInp();
                this.keyword = want.first;
            }
        }
    }
</script>

<style scoped lang="scss">
    .search_page{
        width: 100%;
        background-color: #fbfcfd;
        .userSearch{
            position: relative;
            padding: 15px 10px;
            border-bottom: 1px solid #efefef;
            .userInpDiv {
                margin-top: 9vw;
                position: relative;
                width: 100%;
                height: 30px;
                padding: 0 30px;
                box-sizing: border-box;
                background: #ebecec;
                border-radius: 30px;
            }
            .sousuo{
                position: absolute;
                left: 0;
                top: 5px;
                vertical-align: middle;
                margin: 0 8px;
            }
            #userInp{
                width: 100%;
                height: 30px;
                line-height: 18px;
                background: transparent;
                font-size: 14px;
                color: #333;
                outline: none;
                border: 0;
            }
            .guanbi{
                font-size: 20px;
                color: #b4b4b4;
                position: absolute;
                right: 5px;
                top: 5px;
                vertical-align: middle;
                margin: 0 8px;
            }
        }
        .searchC{
            .hotSearch{
                padding: 15px 10px 0;
                .hot_title{
                    text-align: left;
                    font-size: 12px;
                    line-height: 12px;
                    color: #666;
                    font-weight: 400;
                }
                .hot_list{
                    margin: 10px 0 7px;
                    .hot_item{
                        font-size: 14px;
                        position: relative;
                        display: inline-block;
                        height: 32px;
                        margin: 0 0 8px 8px;
                        padding: 0 14px;
                        line-height: 32px;
                        color: #333;
                        border: 1px solid #d3d4da;
                        border-radius: 32px;
                        float: left;
                        cursor: pointer;
                        a{
                            color: #333;
                        }
                    }
                }
            }
            .searchContent{
                .searchTitle{
                    height: 50px;
                    margin-left: 10px;
                    padding: 0 10px;
                    font-size: 15px;
                    line-height: 50px;
                    color: #507daf;
                    position: relative;
                    overflow: hidden;
                    text-overflow: ellipsis;
                    white-space: nowrap;
                    word-break: normal;
                    font-weight: 400;
                    text-align: left;
                    border-bottom: 1px solid #d3d4da;
                }
                .searchMatch{
                    .SMatch_li{
                        position: relative;
                        height: 45px;
                        line-height: 45px;
                        padding-left: 10px;
                        border-bottom: 1px solid #efefef;
                        text-align: left;
                        vertical-align: middle;
                        i{
                            display: inline-block;
                            margin-right: 7px;
                            font-size: 16px;
                        }
                        .songs_name{
                            position: absolute;
                            width: 90%;
                            display: inline-block;
                            padding-right: 10px;
                            font-size: 15px;
                            line-height: 45px;
                            color: #333;
                            overflow: hidden;
                            text-overflow:ellipsis;
                            white-space: nowrap;
                        }
                    }
                }
            }
            .searchAnswer{
                .searchSongs{
                    text-align: left;
                    color: #333;
                    .songs_li{
                        display: block;
                        padding-left: 10px;
                        position: relative;
                        >div{
                            position: relative;
                            padding: 6px 0;
                            .songN{
                                font-size: 17px;
                                color: #507daf;
                            }
                            .songDec{
                                font-size: 12px;
                                color: #888;
                                overflow: hidden;
                                text-overflow: ellipsis;
                                white-space: nowrap;
                                word-break: normal;
                            }
                            i{
                                color: #888;
                                font-size: 24px;
                                position: absolute;
                                right: 22px;
                                top: 15px;
                            }
                        }
                    }

                }
            }
            .s_singer{
                width: 30%;
                overflow: hidden;
                text-overflow:ellipsis;
                white-space: nowrap;
            }
            .s_singer:not(:last-child)::after{
                content: " / ";
                text-decoration: none;
            }
        }
    }
</style>
